﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZOVNP.Model;
using ZOVNP.Model.Abstracts;

namespace ZOVNP.WinView
{
    public partial class PriceItemEditControl : UserControl
    {
        private IPriceItem _priceItem;
        private Workspace _workspace;

        public PriceItemEditControl(IPriceItem priceItem, Workspace workspace)
        {
            _workspace = workspace;

            cbxMaterial.DataSource = _workspace.GetAllMaterials();
            cbxPrice.DataSource = _workspace.GetAllPrices();
            cbxProduct.DataSource = _workspace.GetAllProducts();

            PriceItem = priceItem;
            InitializeComponent();
        }

        public IPriceItem PriceItem
        {
            get { return _priceItem; }
            set
            {
                _priceItem = value;

                tbxCost.Text = _priceItem != null ? _priceItem.Cost.ToString() : string.Empty;
                cbxColor.SelectedItem = _priceItem != null ? _priceItem.Color : null;
                cbxMaterial.SelectedItem = _priceItem != null ? _priceItem.Material : null;
                cbxPrice.SelectedItem = _priceItem != null ? _priceItem.Price : null;
                cbxProduct.SelectedItem = _priceItem != null ? _priceItem.Product : null;
            }
        }

        private void cbxPrice_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_priceItem == null)
                throw new InvalidOperationException();

            _priceItem.Price = (IPrice)cbxPrice.SelectedItem;
        }

        private void cbxProduct_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_priceItem == null)
                throw new InvalidOperationException();

            _priceItem.Product = (IProduct)cbxProduct.SelectedItem;
        }

        private void cbxMaterial_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_priceItem == null)
                throw new InvalidOperationException();

            _priceItem.Material = (IMaterial)cbxMaterial.SelectedItem;
        }

        private void cbxColor_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_priceItem == null)
                throw new InvalidOperationException();

            _priceItem.Color = (string)cbxColor.SelectedItem;
        }

        private void tbxCost_TextChanged(object sender, EventArgs e)
        {
            if (_priceItem == null)
                throw new InvalidOperationException();

            _priceItem.Cost = Decimal.Parse(tbxCost.Text);
        }
    }
}
